package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.mbg.model.SmsFlashPromotionSession;
import cn.zcscloud.mall.service.SmsFlashPromotionSessionService;
import cn.zcscloud.mall.vo.SmsFlashPromotionSessionDetail;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/flashSession")
public class SmsFlashPromotionSessionController {
    @Autowired
    private SmsFlashPromotionSessionService flashPromotionSessionService;

    //添加场次
    @PostMapping("/create")
    public R create(@RequestBody SmsFlashPromotionSession promotionSession) {
        if (promotionSession == null || promotionSession.getName()  == null || promotionSession.getStartTime() == null || promotionSession.getEndTime()  ==  null){
            return R.error("缺少参数",403);
        }
        boolean save = flashPromotionSessionService.save(promotionSession);
        if (!save) {
            return R.error("添加失败", 500);
        }
        return R.success("");
    }

    //删除场次
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable Long id) {
        boolean b = flashPromotionSessionService.removeById(id);

        return R.success("");
    }

    @GetMapping("/list")
    public R list() {
        List<SmsFlashPromotionSession> list = flashPromotionSessionService.list();
        return R.success(list);
    }

    @GetMapping("/selectList")
    public R selectList(Long flashPromotionId) {
        List<SmsFlashPromotionSessionDetail> promotionSessionList = flashPromotionSessionService.selectList(flashPromotionId);
        return R.success(promotionSessionList);
    }

    @PostMapping("/update/status/{id}")
    public R updateStatus(@PathVariable Long id, Integer status) {
        SmsFlashPromotionSession flashPromotionSession = new SmsFlashPromotionSession();
        flashPromotionSession.setId(id);
        flashPromotionSession.setStatus(status);
        flashPromotionSessionService.updateById(flashPromotionSession);
        return R.success("");
    }

    @PostMapping("/update/{id}")
    public R update(@RequestBody SmsFlashPromotionSession flashPromotionSession) {
        flashPromotionSessionService.updateById(flashPromotionSession);
        return R.success("");
    }

}
